﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class bookLoanManage : Form
    {
        public bookLoanManage()
        {
            InitializeComponent();
        }

        public string borrowerId, bookBarcode,strCardState,strBibliothecaId,loanerName;
        public int maxDay, loanNums;
        public DateTime loanDate, returnDate;
        private void getBorrower()
        {
            DataTable dt = loanClass.getBorrowerInfo(borrowerId);
            strCardState = dt.Rows[0]["借书证状态"].ToString();
            int maxNum = int.Parse(dt.Rows[0]["最大借书数量"].ToString());
            switch (this.strCardState)
            {
                case "有效":
                    if(loanClass.isOverdue(borrowerId)==true)
                    {
                        int loanTotalNum = loanClass.getLoanBookNums(borrowerId);
                        if(loanTotalNum<maxNum)
                        {
                            this.txtBorrowerName.Text = dt.Rows[0]["姓名"].ToString();
                            this.txtCardState.Text = dt.Rows[0]["借书证状态"].ToString();
                            this.txtPermitNums.Text = maxNum.ToString();
                            this.txtLoanNums.Text = loanTotalNum.ToString();
                            this.txtForegift.Text = dt.Rows[0]["押金剩余"].ToString();
                            maxDay = int.Parse(dt.Rows[0]["最长借书期限"].ToString());
                            this.dataGridView1.DataSource = loanClass.getLoanInfo(borrowerId);
                            this.btnLoan.Enabled = true;
                        }
                        else
                        {
                            MessageBox.Show("你已经达到最大借书量！","提示信息");
                            this.txtBorrowerId.Text = "";
                            this.txtBorrowerId.Focus();
                            this.btnLoan.Enabled = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("你已经有超期图书了！","提示信息");
                        //
                        //
                        //
                        this.txtBorrowerId.Text = "";
                        this.txtBorrowerId.Focus();
                        this.btnLoan.Enabled = false;
                    }
                    break;
                case "挂失":
                    MessageBox.Show("借阅者的借书证已被挂失！","提示信息");
                    this.txtBorrowerId.Text = "";
                    this.txtBorrowerId.Focus();
                    this.btnLoan.Enabled = false;
                    break;
                case "停用":
                    MessageBox.Show("借阅者的借书证已被停用！", "提示信息");
                    this.txtBorrowerId.Text = "";
                    this.txtBorrowerId.Focus();
                    this.btnLoan.Enabled = false;
                    break;
            }
        }

        private void getBook()
        {
            DataTable dt = loanClass.getBookInfo(bookBarcode);
            this.txtBookName.Text = dt.Rows[0]["图书名称"].ToString();
            this.txtAuthor.Text = dt.Rows[0]["作者"].ToString();
            this.txtTotalNums.Text = dt.Rows[0]["总藏书数量"].ToString();
            this.txtNowNums.Text = dt.Rows[0]["现存数量"].ToString();
            this.tztPrice.Text = dt.Rows[0]["价格"].ToString();
            strBibliothecaId=dt.Rows[0]["书目编号"].ToString();
        }

        private void setLoanInfo()
        {
            borrowerId = this.txtBorrowerId.Text.Trim();
            bookBarcode = this.txtBookBarcode.Text.Trim();
            loanDate =DateTime.Parse(DateTime.Now.Date.ToShortDateString());
            returnDate =DateTime.Parse(DateTime.Today.AddDays(maxDay).Date.ToShortDateString());
            loanNums = 0;
            loanerName = this.txtBorrowerName.Text.Trim();
        }

        private void bookLoanManage_Load(object sender, EventArgs e)
        {
            this.btnLoan.Enabled = false;
        }

        private void btnSelectBorrowerId_Click(object sender, EventArgs e)
        {
            selectBorrower frmBorrowerSelect = new selectBorrower();
            frmBorrowerSelect.ShowDialog();
            borrowerId = frmBorrowerSelect.getBorrowerId();
            this.txtBorrowerId.Text = borrowerId;
            getBorrower();
        }

        private void btnSelectBookBarcode_Click(object sender, EventArgs e)
        {
            selectBook frmBookSelect = new selectBook();
            frmBookSelect.ShowDialog();
            bookBarcode = frmBookSelect.getBookCode();
            this.txtBookBarcode.Text = bookBarcode;
            getBook();
        }

        private void txtBorrowerId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                borrowerId = this.txtBorrowerId.Text;
                getBorrower();
            }
        }

        private void txtBookBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bookBarcode = this.txtBookBarcode.Text;
                getBook();
            }
        }

        private void btnLoan_Click(object sender, EventArgs e)
        {
            if (this.txtBorrowerId.Text=="")
            {
                MessageBox.Show("借阅者编号不能为空，请选择借阅者编号！");
            }
            if(this.txtBookBarcode.Text=="")
            {
                MessageBox.Show("图书条码不能为空，请选择图书条码！");
            }
            setLoanInfo();
            
            if (loanClass.loanAdd(borrowerId, bookBarcode, loanDate, returnDate, loanNums, loanerName)==true)
            {
                MessageBox.Show("借书成功！", "提示信息");
                loanClass.bookNowNumReduce(strBibliothecaId);
                loanClass.setBookState(bookBarcode, "借出");
                this.btnLoan.Enabled = false;
                this.dataGridView1.DataSource = loanClass.getBorrowerInfo(borrowerId);
                this.dataGridView1.Refresh();
            }
            else
            {
                MessageBox.Show("借书失败，请重试！", "提示信息");
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}